﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Globalization;
using System.Windows;
using DunSilverlight.Comm;

namespace DunSilverlight
{
    public class SexConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return "未知性别";
            }
            else
            {
                return ((bool)value) ? "男" : "女";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }
    public class SexImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return "../Themes/Images/user-silhouette.png";
            }
            else
            {
                return ((bool)value) ? "../Themes/Images/Male.png" : "../Themes/Images/female.png";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }
    public class SexBoxConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (parameter as string).Equals("male") ? value : !(bool)value;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (parameter as string).Equals("male") ? value : !(bool)value;
        }
    }
    public class BoolToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((bool)value) ? Visibility.Visible : Visibility.Collapsed;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }
    public class VisibilityToBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((Visibility)value)==Visibility.Visible ? true : false;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }

    public class VisitRightToEnable : IValueConverter
    {
        /// <summary>
        /// 关联的外访记录
        /// </summary>
        public static DunLibrary.Dun.VisitRecord VisitRecord { get; set; }

        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                int p = int.Parse(parameter.ToString());
                bool isChecked = (bool)value;
                //当前用户是否具有审核权
                bool isCheckRight = Comm.RightManagement.VerifyRight(Comm.RightManagement.Right_Visits_Check);

                int userId = AppGlobal.CurrentUser == null ? 0 : AppGlobal.CurrentUser.Id;//当期用户id
                int operId = VisitRecord.Case.Oper == null ? 0 : VisitRecord.Case.Oper.Id;//外访记录录入人
                int checkerId = VisitRecord.Checker == null ? 0 : VisitRecord.Checker.Id;   //外访记录指定的审核人

                bool result = false;

                switch (p)
                {
                    case 1://录入人审核前可以编辑，审核人都不能编辑
                        result = userId == operId && !isChecked;
                        break;
                    case 2://录入人审核后可以编辑，审核人都不能编辑
                        result = userId == operId && isChecked;
                        break;
                    case 3://审核人审核前可以编辑,审核后不能编辑
                        result = !isChecked && isCheckRight && userId == checkerId;
                        break;
                    case 4://录入人，审核人审核前都可以编辑，审核后不能编辑
                        result = (userId == operId || userId == checkerId) && !isChecked;
                        break;
                }

                return result;
            }
            catch (Exception ex)
            {
                Tools.ShowMessage(ex.Message, "", false);
                return false;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (bool)value;
        }

        #endregion
    }



}
